﻿using System.Collections.Generic;
using System.Linq;
using FluentNHibernate.Mapping;

namespace Model
{
	public class Realm
	{
		public virtual int ID { get; set; }
		public virtual string Name { get; set; }
		public virtual Region Region { get; set; }
		private IList<Guild> _guilds = new List<Guild>();
		public virtual IList<Guild> Guilds { get { return _guilds; }set { _guilds = value; } }

		private IList<Character> _toons = new List<Character>();
		public virtual IList<Character> Toons
		{
			get { return _toons; }
			set
			{
				_toons = value;
				_toonCache = value.ToDictionary(x => x.Name);
			}
		}

		private IDictionary<string, Character> _toonCache = new Dictionary<string, Character>();

		public virtual Character FindToon(string name)
		{
			Character character;
			_toonCache.TryGetValue(name, out character);
			return character;
		}

		public virtual void AddToon(Character character)
		{
			_toons.Add(character);
			_toonCache.Add(character.Name, character);
		}
	}

	public class RealmMapping : ClassMap<Realm>
	{
		public RealmMapping()
		{
			Id(x => x.ID);
			Map(x => x.Name);
			References(x => x.Region);
			HasMany(x => x.Guilds).Not.LazyLoad();
			HasMany(x => x.Toons).Not.LazyLoad();
		}
	}
}